package com.flowerplus.store.app.login;

import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.flowerplus.store.R;
import com.flowerplus.store.common.data.injector.HasComponent;
import com.flowerplus.store.common.data.utils.ui.ToastUtil;
import com.flowerplus.store.common.structure.mvp.BasePresenter;
import com.flowerplus.store.common.structure.ui.activity.BaseActivity;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by dell - on 2018/1/18.
 */

public class MobileInputActivity extends BaseActivity implements MobileInputContract.View, HasComponent<LoginComponet> {
    @BindView(R.id.et_mobile)
    EditText mEtMobile;
    @BindView(R.id.ll_edit)
    LinearLayout mLlEdit;
    @BindView(R.id.v_center)
    View mVCenter;
    @BindView(R.id.tv_confirm)
    TextView mTvConfirm;
    @BindView(R.id.iv_fanhui)
    ImageView mIvFanhui;
    @BindView(R.id.btn_close)
    Button mBtnClose;
    @BindView(R.id.tv_title)
    TextView mTvTitle;
    String MOBILE_REX = "1[3|4|5|7|8|9][0-9]{9}";


    @Inject
    MobileInputPresenter mPresenter;

    LoginComponet mCircularComponent;

    @Override
    protected void initViews() {
        mPresenter.onCreate(this);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initEvents() {
        mEtMobile.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                String mobile = s.toString();

                if (mobile != null && mobile.length() > 0) {
                    mTvConfirm.setEnabled(true);
                } else {
                    mTvConfirm.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

    }

    @Override
    public void initInject() {
        mCircularComponent = DaggerLoginComponet.builder().applicationComponent(getApplicationComponent()).activityModule(getActivityModule()).build();
        mCircularComponent.inject(this);
    }

    @Override
    protected int layoutResId() {
        return R.layout.activity_mobile_input;
    }

    @Override
    protected BasePresenter getPresenter() {
        return null;
    }


    @OnClick({R.id.tv_confirm, R.id.iv_fanhui, R.id.btn_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_confirm:
                final String mobile = mEtMobile.getText().toString().trim();
                if (mobile == null && mobile.length() != 11) {
                    ToastUtil.getToastUtil().showShort(("请输入正确的手机号"));
                    return;
                }
                if (!mobile.matches(MOBILE_REX)) {
                    ToastUtil.getToastUtil().showShort(("请输入正确的手机号"));
                    return;
                }
                mPresenter.checkPhone(mobile);
                break;
            case R.id.iv_fanhui:
                break;
            case R.id.btn_close:
                break;
        }
    }

    @Override
    public LoginComponet getComponent() {
        return mCircularComponent;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void error(String msg) {
        ToastUtil.getToastUtil().showShort(msg);
    }

    @Override
    public void checkPhoneSuccess() {

    }
}
